package ac.zlf.springmybatis.core.util;

import ac.zlf.core.util.constant.PhotoConstantsExt;
import ac.zlf.core.util.constant.PhotoDimension;
import ac.zlf.springmybatis.core.util.image.ImageRotator;
import ac.zlf.springmybatis.core.util.image.ImageScaler;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class ThumbHelper {
    public static void scalePhoto(File dstFile, File srcFile, String dimenString, int orientation)
            throws IOException {
        scalePhoto(dstFile, srcFile, dimenString, orientation, false);
    }

    public static void scalePhoto(File dstFile, File srcFile, String dimenString, int orientation, boolean crop)
            throws IOException {
        if (!srcFile.exists()) {
            return;
        }
        Dimension size = extractDimen(dimenString);
        if (!dstFile.exists()) {
            Image src = Toolkit.getDefaultToolkit().getImage(srcFile.getPath());
            BufferedImage image = toBufferedImage(src);
            if (image == null) {
                return;
            }
            int width = image.getWidth();
            int height = image.getHeight();


            float scale = getRatio(width, height, size.width, size.height);
            width = (int) (scale * width);
            height = (int) (scale * height);
            image = ImageScaler.scaleImage(width, height, image);

            image = ImageRotator.rotateImage(orientation, image);
            if (crop) {
                width = image.getWidth();
                height = image.getHeight();
                if (width > height) {
                    image = image.getSubimage((width - height) / 2, 0, height, height);
                } else if (height > width) {
                    image = image.getSubimage(0, (height - width) / 2, width, width);
                }
            }
            ImageIO.write(image, "jpg", dstFile.getAbsoluteFile());
        }
    }

    public static float getRatio(int width, int height, int maxWidth, int maxHeight) {
        float ratio = 1.0F;


        float widthRatio = maxWidth / width;
        float heightRatio = maxHeight / height;
        if ((widthRatio < 1.0D) || (heightRatio < 1.0D)) {
            ratio = widthRatio <= heightRatio ? heightRatio : widthRatio;
        }
        return ratio;
    }

    public static Dimension extractDimen(String dimenString)
            throws IOException {
        Dimension size = (Dimension) PhotoConstantsExt.scaleMap.get(dimenString);
        if (size == null) {
            if (dimenString.startsWith("64X64")) {
                size = parseSize(dimenString.substring("64X64".length()));
            } else if (dimenString.startsWith("32X32")) {
                size = parseSize(dimenString.substring("32X32".length()));
            }
            if (size == null) {
                throw new IOException("Invalid type:" + dimenString);
            }
        }
        return size;
    }

    public static Dimension parseSize(String dimenString) {
        String[] sizes = dimenString.toLowerCase().split("x");
        if (sizes.length > 1) {
            try {
                int width = Integer.parseInt(sizes[0]);
                int height = Integer.parseInt(sizes[1]);
                return new Dimension(width, height);
            } catch (NumberFormatException e) {
            }
        }
        return null;
    }

    public static BufferedImage toBufferedImage(Image image) {
        if ((image instanceof BufferedImage)) {
            return (BufferedImage) image;
        }
        image = new ImageIcon(image).getImage();
        BufferedImage bimage = null;
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        try {
            int transparency = 1;
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gs.getDefaultConfiguration();
            bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
        } catch (HeadlessException e) {
        }
        if (bimage == null) {
            int type = 1;
            bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
        }
        Graphics g = bimage.createGraphics();

        g.drawImage(image, 0, 0, null);
        g.dispose();
        return bimage;
    }

    public static void main(String[] args) {
        File file = new File("D:\\test\\8m.jpg");
        File smallThumbFile1 = new File("D:\\test\\a_new_1.jpeg");
        File smallThumbFile2 = new File("D:\\test\\a_new_2.jpeg");
        try {
            long c = System.currentTimeMillis();
            scalePhoto(smallThumbFile1, file, PhotoDimension.THUMB_640X640.getValue(), 1, false);
            scalePhoto(smallThumbFile2, file, PhotoDimension.THUMB_150X150.getValue(), 1, false);
            System.out.println("elapse time:" + (float) (System.currentTimeMillis() - c) / 1000.0F + "s");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



/* Location:           D:\repository\cc\hwtech\core\2.0.1-school-SNAPSHOT\core-2.0.1-school-SNAPSHOT.jar

 * Qualified Name:     ThumbHelper

 * JD-Core Version:    0.7.0.1

 */
